//
//  XS_GoalCertCell.swift
//  XueDao
//
//  Created by Mac on 2020/12/15.
//

import UIKit

protocol XS_GoalCertCellDelegate {
    func goalCertCell(goalCertCell : XS_GoalCertCell, didClickCourse : XS_CourseItemModel)
}

class XS_GoalCertCell: UICollectionViewCell {
    
    var delegate : XS_GoalCertCellDelegate?

    @IBOutlet weak var collectionView: UICollectionView!
    
    var _model : XS_CourseItemM!
    public var model : XS_CourseItemM!{
        set{
            _model = newValue
            //设置数据
            collectionView.reloadData()
        }
        get{
            return _model
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
//        collectionView.register(UINib.init(nibName: "XS_GoalCertItemCell", bundle: Bundle.main), forCellWithReuseIdentifier: "XS_GoalCertItemCell")
        collectionView.register(XD_GoalCertItemCell.self, forCellWithReuseIdentifier: "XD_GoalCertItemCell")
        collectionView.delegate = self
        collectionView.dataSource = self
    }

}

extension XS_GoalCertCell : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if model != nil {
            return model.records.count
        }
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XS_GoalCertItemCell", for: indexPath) as! XS_GoalCertItemCell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XD_GoalCertItemCell", for: indexPath) as! XD_GoalCertItemCell
        if model != nil {
            cell.model = model.records[indexPath.item]
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//        return CGSize.init(width: 146, height: 206)
        return CGSize.init(width: .screenWidth - (16 * 2), height: .scale * 104)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if delegate != nil {
            delegate?.goalCertCell(goalCertCell: self, didClickCourse: model.records[indexPath.item])
        }
    }
}
